package com.med.meddataintegrator.admin.service.Impl;

import com.med.meddataintegrator.admin.service.AppointmentService;
import com.med.meddataintegrator.common.mapper.AppointmentDoMapper;
import com.med.meddataintegrator.common.mapper.DoctorMapper;
import com.med.meddataintegrator.common.mapper.PatientDoMapper;
import com.med.meddataintegrator.common.pojo.dos.AppointmentDo;
import com.med.meddataintegrator.common.pojo.dto.Appointment.ApplicationQuery;
import com.med.meddataintegrator.common.pojo.dto.Appointment.ApplicationUpdateParam;
import com.med.meddataintegrator.common.pojo.dto.Appointment.AppointmentAddParam;
import com.med.meddataintegrator.common.pojo.vo.appointment.AppointmentVo;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;

import java.beans.Transient;
import java.util.List;

@Slf4j
@Service
public class AppointmentServiceImpl implements AppointmentService {
    @Autowired
    AppointmentDoMapper appointmentDoMapper;
    @Autowired
    PatientDoMapper patientDoMapper;
    @Autowired
    DoctorMapper doctorMapper;

    @Override
    @Transient
    public void add(AppointmentAddParam appointmentAddParam) {
        log.debug("预约信息", appointmentAddParam);
        AppointmentDo appointmentDo = new AppointmentDo();
        BeanUtils.copyProperties(appointmentAddParam, appointmentDo);
        int patientId = patientDoMapper.selectByPatientName(appointmentAddParam.getPatientName());
        int doctorId = doctorMapper.selectByDoctorName(appointmentAddParam.getDoctorName());
        log.debug("预约信息{}", appointmentDo);

        appointmentDo.setPatientId(patientId);
        appointmentDo.setDoctorId(doctorId);

        appointmentDoMapper.insert(appointmentDo);
    }

    @Override
    public void delete(Integer id) {
        log.debug("删除预约信息", id);
        appointmentDoMapper.deleteById(id);
    }

    @Override
    public void update(ApplicationUpdateParam applicationUpdateParam) {
        log.debug("更新预约信息", applicationUpdateParam);
        int patientId = patientDoMapper.selectByPatientName(applicationUpdateParam.getPatientName());
        int doctorId = doctorMapper.selectByDoctorName(applicationUpdateParam.getDoctorName());
        AppointmentDo appointmentDo = new AppointmentDo();
        appointmentDo.setExpenses(applicationUpdateParam.getExpenses());
        appointmentDo.setStatus(applicationUpdateParam.getStatus());
        appointmentDo.setTime(applicationUpdateParam.getTime());
        appointmentDo.setPatientId(patientId);
        appointmentDo.setDoctorId(doctorId);
        appointmentDoMapper.updateByPrimaryKey(appointmentDo);
    }

    @Override
    public List<AppointmentVo> list(ApplicationQuery applicationQuery) {
        log.debug("查询预约信息", applicationQuery);
        Integer patientId = patientDoMapper.selectByPatientName(applicationQuery.getPatientName());
        Integer doctorId = doctorMapper.selectByDoctorName(applicationQuery.getDoctorName());
        List<AppointmentVo> list = appointmentDoMapper.select(patientId, doctorId);
        return list;
    }


}
